/* 
 * ITextRendererTest.java * 
 * Copyright 2009 Shanghai TuDou.  
 * All rights reserved. 
 */

package com.sp2p.util;

import java.awt.Color;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.struts2.ServletActionContext;
import org.xhtmlrenderer.pdf.ITextFontResolver;
import org.xhtmlrenderer.pdf.ITextRenderer;

import com.lowagie.text.DocumentException;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfStamper;
import com.shove.security.Encrypt;
import com.sp2p.constants.IConstants;

/**
 * TODO class description *
 * 
 * @author pcwang
 * 
 * @version 1.0, 上午11:03:26 create $Id$
 */
public class PDFUtil {

	/**
	 * 增加水印
	 * 
	 * @throws DocumentException
	 * @throws IOException
	 */
	public static void addWater(String urlSource, String urlFor, String path)
			throws Exception {
		// 待加水印的文件
		PdfReader reader = new PdfReader(urlSource);
		// 加完水印的文件
		PdfStamper stamper = new PdfStamper(reader,
				new FileOutputStream(urlFor));
		try {

			int total = reader.getNumberOfPages() + 1;

			PdfContentByte content = null;

//			int high = 50;// 高度
			// 循环对每页插入水印
			for (int i = 1; i < total; i++) {
				// 水印的起始
//				high = 500;
				// content = stamper.getUnderContent(i);//底部添加
				content = stamper.getOverContent(i);// 顶部添加
				// 开始
				content.beginText();
				// 设置颜色
				content.setColorFill(Color.RED);
				// 设置起始位置
				content.setTextMatrix(200, 480);

				// 水印图片
				com.lowagie.text.Image image = com.lowagie.text.Image
						.getInstance(path + "images/testR.png");
				image.setAbsolutePosition(200, 100);
				// 添加图片
				content.addImage(image);

				content.endText();

			}
		} catch (Exception e) {
			e.printStackTrace();
			throw (e);
		} finally {
			stamper.close();
		}

	}

	/**
	 * 创建pdf
	 * 
	 * @param url
	 * @param pdfUrl
	 * @param pdfName
	 * @return
	 * @throws Exception
	 */
	public static String pdfCreate(String url, String pdfUrl, String pdfName,
			String path) throws Exception {
		String pdfUrl2 = path + "upload/pdf/" + pdfName;
		System.out.println("pdfNowName=" + pdfUrl2);
		OutputStream os = new FileOutputStream(pdfUrl2);
		ITextRenderer renderer = new ITextRenderer();
		System.out.println("url---0-0--------"+url);
		renderer.setDocument(url);

		// 解决中文支持问题
		ITextFontResolver fontResolver = renderer.getFontResolver();
		fontResolver.addFont(path + "WEB-INF/fonts/simkai.ttf", BaseFont.IDENTITY_H,
				BaseFont.NOT_EMBEDDED);

		renderer.layout();
		renderer.createPDF(os);
		os.close();
		return "success";
	}

	/**
	 * 搜索pdf存在
	 * 
	 * @param url
	 * @return
	 * @throws Exception
	 */
	public static String pdfSearch(String url,String number) throws Exception {
		//加密文件名
		number =  Encrypt.MD5(number.trim()+IConstants.PASS_KEY);
		String pdfName = number + ".pdf";
		try {
			String path = ServletActionContext.getServletContext().getRealPath("/");
			String pdfUrl = "upload/pdf/" + pdfName;

			File folder = new File(path + "upload/pdf");
			if (!folder.exists()) {
				folder.mkdirs();
				System.out.println("成功创建upload/pdf目录");
			}
			System.out.println("upload/pdf目录已存在");

			File file = new File(path + pdfUrl);
			if (!file.exists()) {
				// 不存在则创建
				System.out.println("pdf不存在！新建url为" + pdfUrl);
				String reStr = pdfCreate(url, path + pdfUrl, pdfName, path);
				if (reStr == "success") {
					return pdfUrl;
				}
				return "fail";
			} else {
				// 存在则返回pdfUrl
				System.out.println("pdf已存在！url为" + pdfUrl);
				return pdfUrl;
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw (e);
		}
	}

}
